package yxy.game.pm2.active.module.subordinateclass;

import yxy.apple.util.redisson.RedisException;
import yxy.game.pm2.bean.db.dao.MySQLCacheDAO;
import yxy.game.pm2.bean.timeline.TimelineModule;

import java.util.ArrayList;

public class GetRedeemCode extends TimelineModule {

    //bean ---------------------------------------
    private String pcode;
    private ArrayList<String> opocbatchs;    //一人一码批次list



    public String getPcode() {
        return pcode;
    }

    public void setPcode(String pcode) {
        this.pcode = pcode;
    }

    public ArrayList<String> getOpocbatchs() {
        return opocbatchs;
    }

    public void setOpocbatchs(ArrayList<String> opocbatchs) {
        this.opocbatchs = opocbatchs;
    }

    //func -------------------------------------------
//	0：opoc:一人一码，一个玩家只能使用一个兑换码，兑换后不能再使用其他兑换码并且该兑换码失效
//	1：opnc:一人多码，一个玩家可以使用多个兑换码，同一个码使用后即失效
//	2：npoc:多人一码，一个兑换码可以被多个玩家使用，玩家领取奖励后，不能再次领取
//	public static final int opoc = 0;
//	public static final int opnc = 1;
//	public static final int npoc = 2;

    static private GetRedeemCode crate(String pcode) {
        GetRedeemCode getRedeemCode = new GetRedeemCode();
        getRedeemCode.setPcode(pcode);
        getRedeemCode.setOpocbatchs(new ArrayList<String>());
        getRedeemCode.set();
        return getRedeemCode;
    }

    //redis ------------------------------------------
    static private String key(String pcode) {
        return String.format("PLAYER:%s:GETREDEEMCODE", pcode);
    }

    public void set() {
        try {
            setv(key(getPcode()), this);
        } catch (RedisException e) {
            e.printStackTrace();
        }
    }

    static public GetRedeemCode get(String pcode) {
        try {
            GetRedeemCode module = getv(key(pcode));
            System.out.println("GetRedeemCode执行get");
            if (module == null) {
                //先查数据库
                module = MySQLCacheDAO.getV(key(pcode), GetRedeemCode.class);

                //数据库不存在初始化
                if (module == null) module = GetRedeemCode.crate(pcode);
            }
            return module;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    //存储到数据库的方法
    public static void saveToDB(String playerCode) {
        try {
            System.out.println("GetRedeemCode执行saveToDB");
            String key = key(playerCode);
            if (touchBucket(key).isExists()) {
                GetRedeemCode getRedeemCode = getv(key(playerCode));
                if (getRedeemCode != null) MySQLCacheDAO.setV(key, getRedeemCode);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //删除缓存
    public static void removeAllFromRedis(String playerCode) {
        try {
            touchBucket(key(playerCode)).delete();
        } catch (RedisException e) {
            e.printStackTrace();
        }
    }
    // lock==========================================================================

    @Override
    public String lockKey() {
        return String.format("lock:%s", key(getPcode()));
    }
}
